package com.lwphk.easydocs.autoconfigure;

import java.util.Map;

import javax.servlet.ServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.lwphk.easydocs.core.BasicParseHandler;
import com.lwphk.easydocs.ui.DocsUIController;


@Configuration
@ConditionalOnClass({RequestMappingHandlerMapping.class,RequestMapping.class,DispatcherServlet.class,ServletRequest.class})
@ConditionalOnProperty(prefix = "easydocs",name = "enable",havingValue = "true")
@ConditionalOnWebApplication
public class EasyDocsAutoConfiguration implements ApplicationContextAware, InitializingBean{
	
	private static final Logger log = LoggerFactory.getLogger(EasyDocsAutoConfiguration.class);
	
	private WebApplicationContext webApplicationContext;
	
	@Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.webApplicationContext = (WebApplicationContext)applicationContext;
    }
		
	public EasyDocsAutoConfiguration() {
		log.info("启动easy docs");
	}
	
	
	
	
	@Bean
	public DocsUIController docUIController() {
		return new DocsUIController();
	}
	
	
	public BasicParseHandler parseHandler() {
		return new BasicParseHandler();
	}
	

	@Override
	public void afterPropertiesSet() throws Exception {

		
		
		RequestMappingHandlerMapping handleMapping = webApplicationContext.getBean(RequestMappingHandlerMapping.class);
		
		Environment env = webApplicationContext.getEnvironment();
		
		
		
		
		
		Map<RequestMappingInfo, HandlerMethod> map = handleMapping.getHandlerMethods();
 
		parseHandler().parseBasicInfo(env,map);
		
		
		
	}
	

	
	
}
